import { reqCategoryList, reqGoodsList, reqBanner, reqFloor } from '@/api';

const state = {
  categoryList: [],
  bannerPic: [],
  floorDate: [],
  searchList: {},
};

const mutations = {
  //商品导航数据
  CATEGORYLIST(store, date) {
    date.splice(-1, 1); //删除最后一个
    store.categoryList = date;
  },
  //轮播图数据
  BANNERDADE(store, data) {
    store.bannerPic = data;
  },

  FLOORDADE(store, data) {
    store.floorDate = data;
  },

  //搜索商品列表
  SEARCHLIST(store, data) {
    store.searchList = data;
  },
};

const actions = {
  //通过api接口调用函数，发送请求 获取导航接口
  async requestCategoryList({ commit }) {
    //三级联动--接口
    const { data } = await reqCategoryList();
    if (data.code === 200) {
      commit('CATEGORYLIST', data.data);
    }
  },
  //获取轮播图接口
  async requireBanner({ commit }) {
    const { data } = await reqBanner();
    if (data.code === 200) {
      commit('BANNERDADE', data.data);
    }
  },
  //获取floor 接口
  async requireFloor({ commit }) {
    const { data } = await reqFloor();
    if (data.code === 200) {
      commit('FLOORDADE', data.data);
    }
  },
  //获取搜索商品列表
  async requireGoods({ commit }, params) {
    const { data } = await reqGoodsList(params);
    if (data.code === 200) {
      commit('SEARCHLIST', data.data);
    }
  },
};

const getters = {
  goodsList(state) {
    return state.searchList.goodsList || [];
  },
  trademarkList(state) {
    return state.searchList.trademarkList || [];
  },
  attrsList(state) {
    return state.searchList.attrsList || [];
  },
};

export default {
  state,
  mutations,
  actions,
  getters,
};
